/*******************************************************************************
Project:	Ripping Yarn, Political Communication
Programmer: Andrew Gooch
Date:		11 April 2017

Program Description:
Assessing balance of treatment assignments & F test
*******************************************************************************/

// F test for Stories experiments
// regressions pretreatment covariates on treatment variates
// then outregs f test and means and SDs
// outsum user generated package is required to output the means and SDs

//Please change directory to fit where you have the data
use "C:\Users\agooch\Dropbox\Dissertation\Data\Experiments\Aggregated Dataset\315.dta " 

// use CD for where you want the outsum tables to go
cd  "C:\Users\agooch\Dropbox\Dissertation\Data\Experiments\Aggregated Dataset"

// age is already coded correctly.

gen d_treatment = .
replace d_treatment = 1 if Gtreatment ==1
replace d_treatment = 0 if Gtreatment !=1

gen d_white = .
replace d_white = 1 if Grace ==1
replace d_white = 0 if Grace !=1

gen d_black = .
replace d_black = 1 if Grace ==2
replace d_black = 0 if Grace !=2

gen d_hispanic = .
replace d_hispanic = 1 if Grace ==3
replace d_hispanic = 0 if Grace !=3

gen d_female = .
replace d_female = 1 if Ggender ==2
replace d_female = 0 if Ggender !=2

gen d_dem = .
replace d_dem = 1 if Gpid3 ==1
replace d_dem = 0 if Gpid3 !=1

gen d_rep = .
replace d_rep = 1 if Gpid3 ==2
replace d_rep = 0 if Gpid3 !=2

gen d_ind = .
replace d_ind = 1 if Gpid3 ==3
replace d_ind = 0 if Gpid3 !=3

gen PID3 = Gpid3
replace PID3 = -1 if Gpid3 == 1
replace PID3 = 1 if Gpid3 == 2
replace PID3 = 0 if Gpid3 == 3
replace PID3 = 0 if Gpid3 == 4


// f test + means and Standard Deviations
logit d_treatment Gage d_white d_black d_hispanic d_female PID3 
testparm Gage d_white d_black d_hispanic d_female PID3 
local ftest = r(p)
outsum Gage d_white d_black d_hispanic d_female PID3 if d_treatment==1 using "Balance_MeansSDs_March.out" , replace ctitle ("Control") bracket addnote("F test p value: `ftest'" )
outsum Gage d_white d_black d_hispanic d_female PID3 if d_treatment==0 using "Balance_MeansSDs_March.out" , append ctitle ("Personalized Story") bracket


clear

// Dependent Var is now 1-3, so modeled with mlogit below
use "C:\Users\agooch\Dropbox\Dissertation\Data\Experiments\Aggregated Dataset\51512.dta " 

// age and treatment are already coded correctly

gen d_white = .
replace d_white = 1 if Grace ==1
replace d_white = 0 if Grace !=1

gen d_black = .
replace d_black = 1 if Grace ==2
replace d_black = 0 if Grace !=2

gen d_hispanic = .
replace d_hispanic = 1 if Grace ==3
replace d_hispanic = 0 if Grace !=3

gen d_female = .
replace d_female = 1 if Ggender ==2
replace d_female = 0 if Ggender !=2

gen PID3 = Gpid3
replace PID3 = -1 if Gpid3 == 1
replace PID3 = 1 if Gpid3 == 2
replace PID3 = 0 if Gpid3 == 3
replace PID3 = 0 if Gpid3 == 4


// f test + means and Standard Deviations
mlogit Gtreatment Gage d_white d_black d_hispanic d_female PID3 
testparm Gage d_white d_black d_hispanic d_female PID3 
local ftest = r(p)
outsum Gage d_white d_black d_hispanic d_female PID3 if Gtreatment==1 using "Balance_MeansSDs_May.out" , replace ctitle ("Control") bracket addnote("F test p value: `ftest'" )
outsum Gage d_white d_black d_hispanic d_female PID3 if Gtreatment==2 using "Balance_MeansSDs_May.out" , append ctitle ("Cue") bracket
outsum Gage d_white d_black d_hispanic d_female PID3 if Gtreatment==3 using "Balance_MeansSDs_May.out" , append ctitle ("Personalized Story") bracket



